package com.naodong.shenluntiku.mvp.view.widget.imagepickeview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.naodong.shenluntiku.R;

import java.util.List;

/**
 * Created by hujie on 17/6/15.
 *
 * 仿微信添加图片
 */

public class ImageAddPickerView extends LinearLayout {


    RecyclerView recyclerView;

    private int maxNum;
    private int oneLineShowNum;
    private boolean isShowDel;
    private boolean isShowAnim;

    private int space;

    @DrawableRes
    private int addLabelRes;

    @DrawableRes
    private int deleteLabelRes;
    /**
     * 默认单行显示数量
     */
    private static final int ONE_LINE_SHOW_NUM = 4;
    /**
     * 默认最多显示多少个
     */
    private static final int MAX_NUM = 9;

    private ImageAddPickerListener imageAddPickerListener;


    public void setImageAddPickerListener(ImageAddPickerListener imageAddPickerListener) {
        this.imageAddPickerListener = imageAddPickerListener;
    }

    public List<String> getSelectList() {
        return imageAddPickerAdapter != null ? imageAddPickerAdapter.getSelectList() : null;
    }

    public void add(String imagePath) {
        if (imageAddPickerAdapter != null) {
            imageAddPickerAdapter.add(imagePath);
        }
    }

    public void replaceAll(List<String> list) {
        if (imageAddPickerAdapter != null) {
            imageAddPickerAdapter.replaceAll(list);
        }
    }

    public void addList(List<String> list) {
        if (imageAddPickerAdapter != null) {
            imageAddPickerAdapter.addList(list);
        }
    }

    private ImageAddPickerAdapter imageAddPickerAdapter;
    private FullyGridLayoutManager gridLayoutManager;


    public ImageAddPickerView(Context context) {
        super(context);
        init(context, null);
    }

    public ImageAddPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ImageAddPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            viewTypedArray(context, attrs);
        }
        recyclerView = new RecyclerView(context);
        addView(recyclerView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    }


    private void viewTypedArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageShowPickerView);
        isShowDel = typedArray.getBoolean(R.styleable.ImageShowPickerView_is_show_del, true);
        isShowAnim = typedArray.getBoolean(R.styleable.ImageShowPickerView_is_show_anim, true);
        addLabelRes = typedArray.getResourceId(R.styleable.ImageShowPickerView_add_label, R.drawable.image_show_piceker_add);
        deleteLabelRes = typedArray.getResourceId(R.styleable.ImageShowPickerView_del_label, R.drawable.image_show_piceker_del);
        oneLineShowNum = typedArray.getInt(R.styleable.ImageShowPickerView_one_line_show_num, ONE_LINE_SHOW_NUM);
        maxNum = typedArray.getInt(R.styleable.ImageShowPickerView_max_num, MAX_NUM);
        space = typedArray.getDimensionPixelSize(R.styleable.ImageShowPickerView_space, dp2px(10));
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void show() {
        if (imageAddPickerAdapter == null) {
            recyclerView.setHasFixedSize(true);
            gridLayoutManager = new FullyGridLayoutManager(getContext(), oneLineShowNum);
            gridLayoutManager.setAutoMeasureEnabled(true);
            recyclerView.addItemDecoration(new GridDividerItemDecoration(space, Color.parseColor("#00000000")));
            recyclerView.setLayoutManager(gridLayoutManager);
            imageAddPickerAdapter = new ImageAddPickerAdapter(getContext());
            imageAddPickerAdapter.setImageAddPickerListener(imageAddPickerListener);
            imageAddPickerAdapter.setAddLabelRes(addLabelRes);
            imageAddPickerAdapter.setDeleteLabelRes(deleteLabelRes);
            imageAddPickerAdapter.setMaxNum(maxNum);
            imageAddPickerAdapter.setShowAnim(isShowAnim);
            imageAddPickerAdapter.setShowDel(isShowDel);
            recyclerView.setAdapter(imageAddPickerAdapter);
        }
    }

    public int getMaxNum() {
        return maxNum;
    }

    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public int dp2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
